package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestKeyEvent {


    private JFrame jf;
    private JTextArea jta;

    public TestKeyEvent() {
        jf=new JFrame();
        jta=new JTextArea(20,40);
    }

    private void init(){
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jsp);
    }

    private void setSytle(){
        jta.setFont(new Font("宋体",Font.BOLD,26));
        jta.setForeground(Color.RED);
    }

    private void addEventHandler(){

        jta.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                jta.append(KeyEvent.getKeyText(e.getKeyCode())+"键被按下\n");
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

    }

    public void show(){
        init();
        setSytle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestKeyEvent().show();
    }
}
